<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>云爱伊</title>

    {{template "header"}}


    <style type="text/css">
        /*.layui-table-cell {
            height: auto;
            overflow: visible;
            text-overflow: inherit;
            white-space: normal;
        }*/
        .layadmin-iframe {
            /* position: absolute; */
            width: 100%;
            height: 750px;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
        }
    </style>
</head>
<body>

<div class="layui-container">
    <div class="layui-row" style="text-align: center">
        <div style="width: 100%;text-align: center">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>{{.Phone}}的云爱伊</legend>
            </fieldset>
            {{if .Phone}}
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius"
                        style="margin-top: -85px;margin-left: 550px; " id="loginoutbt">退出登陆
                </button>
            {{end}}

            <blockquote class="layui-elem-quote layui-text " style="font-size: 16px;font-family: 新宋体">
                脱单恋爱神器，用云爱伊准时准确表达心意。<br>
                定时短信发送给那个关心的人，让温暖不迟到。<br>
            </blockquote>

            <div class="layui-tab">
                <ul class="layui-tab-title">
                    <li class="layui-this">云爱伊定时任务</li>
                    <li>个人中心</li>
                    <li>我的短信记录</li>
                    {{if eq "admin" .PhoneAll}}
                        <li>系统管理</li>
                    {{end}}
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-form-pane">
                            <table class="layui-table layui-hide" id="test" lay-filter="test"></table>

                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <iframe src="/User" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"
                                allowtransparency="yes" class="layadmin-iframe"></iframe>
                    </div>
                    <div class="layui-tab-item">
                        <iframe src="/MyHistory" frameborder="no" border="0" marginwidth="0" marginheight="0"
                                scrolling="no"
                                allowtransparency="yes" class="layadmin-iframe"></iframe>
                    </div>
                    {{if eq "admin" .PhoneAll}}
                        <div class="layui-tab-item">
                            <iframe src="/Manager" frameborder="no" border="0" marginwidth="0" marginheight="0"
                                    scrolling="no"
                                    allowtransparency="yes" class="layadmin-iframe"></iframe>
                        </div>
                    {{end}}
                </div>
            </div>


        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container" style="position: absolute;margin-left: 5px;">
        <button class="layui-btn layui-btn" lay-event="add">添加任务</button>
    </div>
</script>

{{template "footer"}}
<script type="application/javascript">
    layui.use('element', function () {
        var $ = layui.jquery
            , element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块

        //触发事件
        var active = {
            tabAdd: function () {
            }
            , tabDelete: function (othis) {

            }
            , tabChange: function () {
            }
        };
        element.on('tab', function (data) {
            console.log("重新加载当前页面")
            var src = $(".layui-tab-item.layui-show").find("iframe").attr("src");
            $(".layui-tab-item.layui-show").find("iframe").attr("src", src);
        });
    });
    layui.use('laytpl', function () {
        var laytpl = layui.laytpl;

    });
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#test',
            url: '/Page',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            title: '用户数据表',
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            cols: [[
                {field: 'id', title: '序号', sort: true, align: 'center', width: 70},
                {field: 'add_user_phone', title: '账号', align: 'center', width: 120,hide: {{ne "admin" .PhoneAll}} },

                {field: 'user_from_name', title: '发送人姓名', align: 'center', width: 100},
                {field: 'time', title: '发送时间', align: 'center', width: 100},
                {field: 'user_to_name', title: '收信人姓名', align: 'center', width: 110},
                {field: 'user_to_phone', title: '收信人电话', align: 'center', width: 120},

                {
                    field: 'status', title: '今日执行状态', align: 'center', width: 120, templet: function (d) {
                        //console.log("1", d, d.status, typeof (d.status))
                        return '<span class="layui-badge ' + ((d.status == 0 ? "layui-bg-orange" : "layui-bg-blue")) + ' "> ' + (d.status == 0 ? "爱意尚未传达" : "爱意已传达") + ' </span>'
                    }
                },
                {
                    field: 'smstype', title: '短信类型', align: 'center', width: 100, templet: function (d) {
                        //console.log("1", d, d.status, typeof (d.status))
                        var smstypestr = ""
                        var colorstr = "layui-bg-blue"
                        if (d.smstype == 1) {
                            smstypestr = "名言警句"
                            colorstr = "layui-bg-green"
                        } else if (d.smstype == 2) {
                            smstypestr = "土味情话"
                            colorstr = "layui-bg-red"
                        } else {
                            smstypestr = "未知"
                        }
                        return '<span class="layui-badge ' + colorstr + ' "> ' + (smstypestr) + ' </span>'
                    }
                },

                {
                    fixed: 'right', title: '操作', rowspan: 3, align: 'center', templet: function (d) {
                        {{if eq "admin" .PhoneAll}}
                        return "  <a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"start\">" + (d.state == 0 ? "已停止" : "运行中") + "</a>       " +
                            "  <a    class=\"layui-btn layui-btn-xs layui-btn-warm\" lay-event=\"run\">强制执行</a>\n         " +
                            "  <a class=\"layui-btn layui-btn-xs\" lay-event=\"edit\">编辑</a>\n         " +
                            "  <a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> "
                        {{else}}
                        return "  <a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"start\">" + (d.state == 0 ? "已停止" : "运行中") + "</a>       " +
                            "  <a class=\"layui-btn layui-btn-xs\" lay-event=\"edit\">编辑</a>\n         " +
                            "  <a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> "
                        {{end}}

                    }
                }
            ]],
            page: true, limits: [20, 50, 100],
            limit: 100,
        });
        //头工具栏事件 table标签里 lay-filter="test"
        table.on('toolbar(test)', function (obj) {
            // console.log(1, "table.on('toolbar(test)'")

            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    layer_show('添加定时任务', '/AddOrUpdateJobPage', 800, 500)
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var param = obj.data;

            if (obj.event === 'del') {
                layer.confirm('确认删除该任务吗?', function (index) {
                    ajaxPostNoAsync("/DeleteJob", {"id": param.id, "domain": param.domain}, function (data) {
                        console.log("DeleteJob:", data, typeof (data))
                        if (data == "success") {
                            obj.del();
                            layer.msg('操作成功!', {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                //do something
                                layer.close(index);
                            });
                        } else {
                            layer.msg('操作失败!', {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                //do something
                            });
                        }
                    })
                });
            } else if (obj.event === 'edit') {
                layer_show('编辑定时任务', '/AddOrUpdateJobPage?id=' + param.id, 800, 500)
            } else if (obj.event === 'start') {
                var info = (param.state == 0 ? "启动" : "关闭")
                layer.confirm('确认' + info + '该任务吗?', function (index) {
                    ajaxPostNoAsync("/ChangeState", {
                        "id": param.id,
                        "state": param.state,
                        "domain": param.domain,
                        "cron": param.cron
                    }, function (data) {
                        console.log("ChangeState:", data)
                        if (data == "success") {
                            layer.msg('操作成功!', {
                                icon: 1,
                                time: 1500 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                window.parent.location.reload();
                            });
                        } else {
                            layer.msg('操作失败!', {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                //do something
                            });
                        }
                    })
                });
            } else if (obj.event == 'run') {
                layer.confirm('确认强制发送短信吗?', function (index) {
                    ajaxPostNoAsync("/RunOne", {
                        "id": param.id,
                    }, function (data) {
                        //console.log("RunOne:", data, typeof (data), data.code == "success")
                        if (data == "success") {
                            layer.msg('操作成功!', {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                            });
                        } else {
                            layer.msg('操作失败!', {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                //do something
                            });
                        }
                    })
                });
            }
        });

    });

    //保存配置信息
    $("#loginoutbt").click(function () {
        window.location.href = "/LoginOut"
    })


</script>
</body>
</html>